package people.paper.tv.activity.home.modual.rmrb.view.impl;

import android.content.Context;
import android.util.Log;
import android.view.View;

import java.util.List;

import people.paper.tv.BaseApplication;
import people.paper.tv.activity.BaseActivity;
import people.paper.tv.activity.home.HomeActivity;
import people.paper.tv.activity.home.HomeFragment;
import people.paper.tv.activity.home.modual.rmrb.presenter.RMRBPresenter;
import people.paper.tv.activity.home.modual.rmrb.view.RMRBView;
import people.paper.tv.activity.home.modual.rmrb.view.data.DailyData;
import people.paper.tv.activity.home.modual.rmrb.view.data.DateListData;
import people.paper.tv.activity.home.modual.ywyl.view.impl.IViewCallBack;

public class RMRBViewImpl implements RMRBView, IRMRBViewCallBack{
    Context context;
    RMRBPresenter presenter;
    public HomeFragment homeFragment;
    RMRBLayout layout;
    boolean isDateLoaded = false;

    @Override
    public View create(Context context, RMRBPresenter presenter, HomeFragment homeFragment) {
        this.context = context;
        this.presenter = presenter;
        this.homeFragment = homeFragment;
        layout = new RMRBLayout(context);
        layout.setClipChildren(false);
        layout.setClipToPadding(false);
        layout.setParentViewCallback(this);
        Log.i("fragment", "rmrb showDataPick 2:"+this);
        return layout;
    }

    @Override
    public boolean changeFragmentFocus(final boolean isLeft) {
        Log.i("fragment", "rmrb changeFragmentFocus:"+isDateLoaded);
        if(isDateLoaded && layout != null){
            return layout.changeFocus(isLeft);
        }else {
            return false;
        }
    }

    @Override
    public boolean setLayoutFocus() {
        return layout.changeFocus(true);
    }

    public void showDataPick() {
        Log.i("fragment", "rmrb showDataPick:"+layout);
        layout.showDataPick(true);
    }

    @Override
    public void resume() {
        if(layout != null)
            layout.resume();
    }

    @Override
    public void pause() {
        if(layout != null)
            layout.pause();
    }

    @Override
    public void onActivityResume() {
        if(layout != null)
            layout.resume();
    }

    @Override
    public void onActivityPause() {
        if(layout != null)
            layout.pause();
    }

    @Override
    public void destroy() {
        if(layout != null)
            layout.destroy();
    }

    @Override
    public RMRBViewImpl getView() {
        return this;
    }

    @Override
    public void onDailyLoaded(final DailyData datas) {
        BaseApplication.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isDateLoaded = true;
                if(datas != null && datas.layoutDatas != null && datas.layoutDatas.size() > 0){
                    layout.refreshDaily(datas);
                }else {
                    layout.setLoadOrErrorStatus(false);
                    layout.showError();
                    layout.hideLoading();
                }
            }
        });
    }

    @Override
    public void showError(int errCode) {
        BaseApplication.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                layout.setLoadOrErrorStatus(false);
                layout.showError();
                layout.hideLoading();
            }
        });
    }

    @Override
    public void onDateLoaded(final List<DateListData> dateList) {
        BaseApplication.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                layout.refreshDate(dateList);
            }
        });
    }

    @Override
    public void setCurPlayId(final int layoutid, final int position) {
        Log.i("rmrb", "rmrb setCurPlayId:"+layoutid+"; "+position);

        BaseApplication.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                layout.refreshCurPlayId(layoutid, position);
            }
        });
    }

    @Override
    public void preFragment(){
    }

    @Override
    public void nextFragment() {
        homeFragment.next(HomeActivity.RMRB_INDEX);
    }

    @Override
    public void getNextPageData(int page) {
        presenter.reload();
    }

    @Override
    public void changeFocus() {
        homeFragment.homeView.changeFocusToBar(HomeActivity.RMRB_INDEX);
    }

    @Override
    public View getParentView() {
        return layout;
    }

    @Override
    public void pauseAudio(){
        presenter.pauseTalk();
    }
}
